package com.yan.gulimall.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yan.common.utils.PageUtils;
import com.yan.common.utils.Query;
import com.yan.common.utils.R;
import com.yan.gulimall.product.dao.SkuInfoDao;
import com.yan.gulimall.product.entity.SkuImagesEntity;
import com.yan.gulimall.product.entity.SkuInfoEntity;
import com.yan.gulimall.product.entity.SpuInfoDescEntity;
import com.yan.gulimall.product.feign.SeckillFeignService;
import com.yan.gulimall.product.service.*;
import com.yan.gulimall.product.vo.SeckillInfoVo;
import com.yan.gulimall.product.vo.SkuItemSaleAttrVo;
import com.yan.gulimall.product.vo.SkuItemVo;
import com.yan.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    SeckillFeignService seckillFeignService;

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {

        List<SkuInfoEntity> list = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));

        return null;
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        /**
         * 异步编排SkuItem
         */
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            // 1、sku标题等文本信息
            SkuInfoEntity skuInfo = getById(skuId);
            skuItemVo.setInfo(skuInfo);
            return skuInfo;
        }, executor);

        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            // 2、获取spu销售属性组合
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttrVos(saleAttrVos);
        }, executor);

        CompletableFuture<Void> spuInfoEntityFuture = infoFuture.thenAcceptAsync((res) -> {
            // 3、获取spu介绍
            SpuInfoDescEntity spuInfoEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesp(spuInfoEntity);
        }, executor);

        CompletableFuture<Void> groupsFuture = infoFuture.thenAcceptAsync((res) -> {
            // 4、获取spu的规格参数
            List<SpuItemAttrGroupVo> groups = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(groups);
        }, executor);

        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
            // 独立的异步任务,获取图片
            List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);

        // 获取商品是否有秒杀价格
        CompletableFuture<Void> secKillFuture = CompletableFuture.runAsync(() -> {
            R skuSecKillInfo = seckillFeignService.getSkuSecKillInfo(skuId);
            if (skuSecKillInfo.getCode() == 0) {
                SeckillInfoVo data = skuSecKillInfo.getData(new TypeReference<SeckillInfoVo>() {
                });
                skuItemVo.setSeckillInfo(data);
            }
        }, executor);


        // 等待所有任务都完成
        CompletableFuture.allOf(saleAttrFuture, spuInfoEntityFuture, groupsFuture, imagesFuture,secKillFuture).get();

        return skuItemVo;
    }

}